<template>
	<view class="container">
		<uni-list v-if="showStatus">
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'phone-filled'}" title="猪只耳号"
							:rightText="pigList.pigCode" />
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'location-filled'}" title="猪只品种"
							:rightText="pigBreed" />
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'person-filled'}" title="猪只类型"
							:rightText="pigType" />
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'phone-filled'}" title="猪只状态"
						   :rightText="pigStatus" />
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'location-filled'}" title="栏舍类型"
						   :rightText="phouseType" />
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'person-filled'}" title="栏舍编码"
						   :rightText="pigList.phouseCode" />
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'phone-filled'}" title="所属批次"
						   :rightText="pigList.pigBatch+'次'" />
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'tune-filled'}" title="已产胎次（次）"
						   :rightText="pigList.pigParity+' 次'" />
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'phone-filled'}" title="猪只来源"
						   :rightText="pigList.pigOrigin" />
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'calendar-filled'}" title="出生日期"
							:rightText="pigList.pigBirthday" />
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'calendar-filled'}" title="入场日期"
						   :rightText="pigList.pigIncomeTime" />
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'location-filled'}" title="出生体重"
						   :rightText="pigList.pigBirthWeight +'kg'" />
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'tune-filled'}" title="乳头数（左）"
						   :rightText="pigList.pigNippleLeftNum+' 个'" />
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'settings-filled'}" title="乳头数（右）"
						   :rightText="pigList.pigNippleRightNum+' 个'" />
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'chat-filled'}" title="备注">
				<template v-slot:footer>
					<input type="textarea" v-model="pigList.remark"
						style="font-size: 12px;color: #999999;text-align: right;" />
				</template>
			</uni-list-item>

		</uni-list>
		<view class="button-group" v-if="showStatus">
			<button type="warn" plain="true" @click="pigDelete" style="width: 45%;border: #E53F32 solid 1px;">删 除</button>
			<button type="primary" plain="ture" @click="pigEdit" style="width: 45%;">修 改</button>
		</view>

		<!-- 修改 -->
		<view class="example" v-if="!showStatus">
		  <uni-forms ref="form" :model="pigForm"   :rules="rules" labelWidth="80px">
		    <uni-forms-item label="猪只耳号" name="pigCode" required>
		      <uni-easyinput v-model="pigForm.pigCode" placeholder="请输入猪只耳号" />
		    </uni-forms-item>
			<uni-forms-item label="猪只品种" name="pigBreed" required>
			  <uni-data-select v-model="pigForm.pigBreed" :placeholder="pigBreed"
			  	:localdata="pigBreeds"
			    ></uni-data-select>
			</uni-forms-item>
			  <uni-forms-item label="猪只类型" name="pigType" required>
				  <uni-data-select v-model="pigForm.pigType" :placeholder="pigType"
				   :localdata="pigTypes"
				  ></uni-data-select>
			  </uni-forms-item>
			  <uni-forms-item label="猪只状态" name="pigStatus" required>
				  <uni-data-select v-model="pigForm.pigStatus" :placeholder="pigStatus"
								   :localdata="pigStatuses"
				  ></uni-data-select>
			  </uni-forms-item>
			  <uni-forms-item label="栏舍类型" name="phouseType" required>
				  <uni-data-select v-model="pigForm.phouseType" :placeholder="phouseType"
								   :localdata="phouseTypes"
				  ></uni-data-select>
			  </uni-forms-item>
			<uni-forms-item label="栏舍编码" name="phouseCode" required>
			  <uni-easyinput v-model="pigForm.phouseCode" placeholder="请输入栏舍编码" />
			</uni-forms-item>
			  <uni-forms-item label="所属批次" name="pigBatch" required>
				  <uni-easyinput v-model="pigForm.pigBatch" placeholder="请输入所属批次" />
			  </uni-forms-item>

			  <uni-forms-item label="已产胎次" name="pigParity" required>
				  <uni-easyinput type="number" v-model="pigForm.pigParity" placeholder="请输入已产胎次（次）" />
			  </uni-forms-item>
			  <uni-forms-item label="猪只来源" name="pigOrigin" required>
				  <uni-data-select v-model="pigForm.pigOrigin" :placeholder="pigOrigin"
								   :localdata="pigOrigins"
				  ></uni-data-select>
			  </uni-forms-item>

			  <uni-forms-item label="出生日期" name="pigBirthday" required>
				  <uni-datetime-picker type="datetime" v-model="pigForm.pigBirthday"/>
			  </uni-forms-item>
			  <uni-forms-item label="入场日期" name="pigIncomeTime" required>
			  <uni-datetime-picker type="datetime" v-model="pigForm.pigIncomeTime"/>
			</uni-forms-item>
			  <uni-forms-item label="出生体重" name="pigBirthWeight" required>
				  <uni-easyinput type="number" v-model="pigForm.pigBirthWeight" placeholder="请输入出生体重(kg)" />
			  </uni-forms-item>
			  <uni-forms-item label="左乳头数" name="pigNippleLeftNum" required>
				  <uni-easyinput type="number" v-model="pigForm.pigNippleLeftNum" placeholder="请输入乳头数（左）(个)" />
			  </uni-forms-item>
			  <uni-forms-item label="右乳头数" name="pigNippleRightNum" required>
				  <uni-easyinput type="number" v-model="pigForm.pigNippleRightNum" placeholder="请输入乳头数（右）(个)" />
			  </uni-forms-item>
			<uni-forms-item label="备注">
					<uni-easyinput type="textarea" v-model="pigForm.remark" placeholder="请输入备注" />
				</uni-forms-item>
			  </uni-forms>
			  <view class="edit-button-group">
			  	<button type="primary" @click="back" style="width: 45%;">返 回</button>
			  	<button type="primary" @click="editPig('form')" style="width: 45%;">提 交</button>
			  </view>
			</view>
	</view>
</template>

<script>

import { listPig,getPig,getPigByPigCode, delPig, addPig, updatePig} from "@/api/hmy/pig";

	export default {
		data() {
			return {
				showStatus:true,
				pigCode:'',
				pigId: '',
				pigList: {},
				pigBreed: '',
				pigBatch:'',
				pigType:'',
				phouseType:'',
				pigStatus:'',
				pigParity:'',
				pigForm:'',
				pigOrigin:'',
				pigNippleLeftNum:'',
				pigNippleRightNum:'',
				remark:'',
				//猪只品种
				pigBreeds:[
					{ value: 1, text: '大白'},
					{ value: 2, text: '长白'},
					{ value: 3, text: '二元'},
					{ value: 4, text: '三元'},
				],
				//猪只类型
				pigTypes:[
					{ value: 1, text: '种猪'},
					{ value: 2, text: '母猪'},
					{ value: 3, text: '肉猪'},
				],
				//栏舍类型
				phouseTypes:[
					{ value: 1, text: '公猪舍'},
					{ value: 2, text: '母猪舍'},
					{ value: 3, text: '肉猪舍'},
				],
				//猪只状态
				pigStatuses:[
					{ value: 1, text: '未进场'},
					{ value: 2, text: '已进场'},
					{ value: 3, text: '死亡'},
					{ value: 4, text: '生病'},
				],
				//猪只来源
				pigOrigins:[
					{ value: 1, text:'购买'},
					{ value: 2, text: '自繁'}
				],


				rules: {
					pigCode: {
						rules: [{required: true, errorMessage: '猪只耳号不能为空'}]
					},
					pigType: {
						rules: [{required: true, errorMessage: '猪只类型不可为空'}]
					},
					phouseType: {
						rules: [{required: true, errorMessage: '栏舍类型不能为空'}]
					},
					phouseCode: {
				  		rules: [{required: true, errorMessage: '栏舍编码能为空'}]
					},
					pigBirthday: {
						rules: [{required: true, errorMessage: '出生时间不能为空'}]
					}
				}
			}
		},
		onLoad(option) { //option为object类型，会序列化上个页面传递的参数
			console.log('option',option);
			this.pigId = option.id;
			console.log('id', this.pigId);
			this.pigCode = option.pigCode;
			console.log('code', this.pigCode)
			if(this.pigCode != undefined){
				console.log(1)
				this.getPigByPigCodeInfo();
			}else{
				console.log(2)
				this.getPigInfo();
			}
			
			
			
		},

		methods: {
			getPigInfo() {
				getPig(this.pigId).then(response => {
					this.pigForm = response.data;
					this.pigList = response.data;
					console.log(1, this.pigList.pigType)
					// 字典类型  猪只品种
					if (this.pigList.pigBreed == 1) {
						this.pigBreed = '大白'
					} else if (this.pigList.pigBreed == 2) {
						this.pigBreed = '长白'
					}else if (this.pigList.pigBreed == 3) {
						this.pigBreed = '二元'
					}else if (this.pigList.pigBreed == 4) {
						this.pigBreed = '二元'
					}
					  // 字典类型  猪只 类型
					if (this.pigList.pigType == 1) {
						this.pigType = '种猪'
					} else if (this.pigList.pigType == 2) {
						this.pigType = '母猪'
					}else if (this.pigList.pigType == 3) {
						this.pigType = '肉猪'
					}
					// 字典类型  栏舍类型
					if (this.pigList.phouseType == 1) {
						this.phouseType = '公猪舍'
					} else if (this.pigList.phouseType == 2) {
						this.phouseType = '母猪舍'
					}else if (this.pigList.phouseType == 3) {
						this.phouseType = '肉猪舍'
					}
					// 猪只状态
					if (this.pigList.pigStatus == 1) {
						this.pigStatus = '未进场'
					} else if (this.pigList.pigStatus == 2) {
						this.pigStatus = '已进场'
					}else if (this.pigList.pigStatus == 3) {
						this.pigStatus = '死亡'
					}else if (this.pigList.pigStatus == 4) {
						this.pigStatus = '生病'
					}
					// 猪只来源
					if (this.pigList.pigOrigin == 1) {
						this.pigOrigin = '购买'
					} else if (this.pigList.pigOrigin == 2) {
						this.pigOrigin = '自繁'
					}

					console.log(response)

				});
				
			},
			getPigByPigCodeInfo() {
				console.log(3, this.pigCode)
				getPigByPigCode(this.pigCode).then(response => {
					this.pigForm = response.data;
					this.pigList = response.data;
					// 字典类型  猪只品种
					if (this.pigList.pigBreed == 1) {
						this.pigBreed = '大白'
					} else if (this.pigList.pigBreed == 2) {
						this.pigBreed = '长白'
					}else if (this.pigList.pigBreed == 3) {
						this.pigBreed = '二元'
					}else if (this.pigList.pigBreed == 4) {
						this.pigBreed = '二元'
					}
					  // 字典类型  猪只 类型
					if (this.pigList.pigType == 1) {
						this.pigType = '种猪'
					} else if (this.pigList.pigType == 2) {
						this.pigType = '母猪'
					}else if (this.pigList.pigType == 3) {
						this.pigType = '肉猪'
					}
					// 字典类型  栏舍类型
					if (this.pigList.phouseType == 1) {
						this.phouseType = '种猪舍'
					} else if (this.pigList.phouseType == 2) {
						this.phouseType = '母猪舍'
					}else if (this.pigList.phouseType == 3) {
						this.phouseType = '肉猪舍'
					}
					// 猪只状态
					if (this.pigList.pigStatus == 1) {
						this.pigStatus = '未进场'
					} else if (this.pigList.pigStatus == 2) {
						this.pigStatus = '已进场'
					}else if (this.pigList.pigStatus == 3) {
						this.pigStatus = '死亡'
					}else if (this.pigList.pigStatus == 4) {
						this.pigStatus = '生病'
					}
					// 猪只来源
					if (this.pigList.pigOrigin == 1) {
						this.pigOrigin = '购买'
					} else if (this.pigList.pigOrigin == 2) {
						this.pigOrigin = '自繁'
					}

					console.log(response)

				});
				
			},
			

			//删除
			pigDelete() {
				console.log("delete", this.pigList.pigId)
				this.$modal.confirm('是否确认删除猪只档案编号为"' +this.pigList.pigId + '"的数据项？').then(() => {
					return delPig(this.pigList.pigId);
				}).then(response => {
					this.$modal.msgSuccess("删除成功");
					this.$tab.navigateTo('/pages/hmy/pig/index');
				}).catch(()=>{

				});
			},

			//修改
			pigEdit() {
				console.log("edit", this.pigList.pigId)
				this.showStatus = !this.showStatus
			},
			//返回
			back(){
				this.showStatus = !this.showStatus
				this.getPig();
			},

			//修改提交
			editPig(form) {
			  this.$refs[form].validate().then(res => {
			    updatePig(this.pigForm).then(response => {
			      this.$modal.msgSuccess("修改成功");
				  setTimeout(()=>{
				  	this.$tab.navigateTo('/pages/hmy/pig/index');
				  },1000)

			    })
			  })
			}
		}
	}
</script>

<style>
	page {
		background-color: #ffffff;
	}

	.button-group {
		display: flex;
		justify-content: space-around;
		width:100%;
	}

	.example {
	  padding: 15px;
	  background-color: #fff;
	}
	.edit-button-group{
		display: flex;
		justify-content: space-around;
	}
</style>
